home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tch_tpas.zip / TL14.TXT < prev    next >
Text File  |  1986-04-05  |  7KB  |  253 lines

  1. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 65
  2.  
  3.  
  4. TURBO-LESSON 14:   INTRODUCTION TO PROCEDURES
  5.  
  6. OBJECTIVES - In this lesson, you will learn about:
  7.  
  8. 1.  PROCEDURE declaration
  9. 2.  Using a Procedure
  10. 3.  Using Parameters
  11. 4.  A counter with error checking
  12.  
  13.  
  14. 1.  PROCEDURE declaration.
  15.  
  16. The PROCEDURE subprogram is similar to the FUNCTION subprogram 
  17. introduced in an earlier lesson.   The form of the declaration 
  18. is:
  19.  
  20. PROCEDURE Add(No_1, No_2 : Integer; VAR Sum : Integer);
  21.  
  22. BEGIN
  23.   Sum := No_1 + No_2;
  24. END;
  25.  
  26. Add is the name of the Procedure.
  27.  
  28. No_1, No_2, and Sum are integer variables called "parameters".
  29.  
  30. VAR in front of Sum indicates that this parameter is a "two-way 
  31. street".  It can receive data from the calling program, and 
  32. return data to the calling program.  No_1 and No_2 can only 
  33. receive data from the calling program. 
  34.  
  35. The BEGIN END block defines the processing performed by the 
  36. procedure:  
  37.  
  38. Add the value in the memory location, No_1, to the value in 
  39. memory location, No_2, and place the result in the memory 
  40. location, Sum.  
  41. î
  42. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 66
  43.  
  44.  
  45. 2.  Using a Procedure.
  46.  
  47. A reference to this procedure in the main program (or another 
  48. procedure or function) would have the following form:
  49.  
  50. Add(2, Count, Adjusted_Count);
  51.  
  52. The procedure is "called" or utilized by simply using its name as 
  53. a statement.  (When you define a Procedure or Function, you are 
  54. adding additional "statements" to your programming language.) 
  55.  
  56. Notice that there are three "parameters" here, and three in the 
  57. Procedure declaration.  The three here are associated with the 
  58. ones in the declaration by position.  
  59.  
  60. The first parameter here, the integer, 2, provides input to the 
  61. first parameter of the procedure, No_1.  
  62.  
  63. The second, the variable, Count, provides input to the second 
  64. parameter of the procedure, No_2.  
  65.  
  66. The third, Adjusted_Count, provides input to the third 
  67. parameter of the procedure, Sum.  
  68.  
  69. Since Sum is declared VAR, variable, it also provides output back 
  70. to Adjusted_Count.  
  71.  
  72.  In the Main Program     In Procedure Add
  73.  
  74.                2 ----------> No_1    { When Procedure      }
  75.            Count ----------> No_2    {    is called        }
  76.   Adjusted_Count ----------> Sum     {                     }
  77. ---------------------------------------------------------------
  78.   Adjusted_Count <---------- Sum     { When Procedure ends }
  79.  
  80. ##### DO:
  81.  
  82. Inspect PROG14.  
  83.  
  84. Run the program.
  85.  
  86. ##### DO:
  87.  
  88. Add the following statement as the first statement in the main 
  89. program:
  90.  
  91. Sum := 10;
  92.  
  93. Run the program.
  94.  
  95. Does it make any difference what value is stored in Sum before 
  96. Procedure Add is referenced?  Look at the procedure - is Sum used 
  97. as an input for the calculation, or only as a result?
  98. î
  99. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 67
  100.  
  101.  
  102. 3.  Using Parameters.
  103.  
  104. You have already been using parameters, but in this section, you 
  105. will see a little more of the power and flexibility of 
  106. parameters.
  107.  
  108. ##### DO:
  109.  
  110. Change the Add statement in the main program to:
  111.  
  112. Add(2, 2, Adjusted_Count);
  113.  
  114. Run the program.    Is the result as expected?
  115.  
  116. ##### DO:
  117.  
  118. Change the Add to:
  119.  
  120. Add(Count, Count, Adjusted_Count);
  121.  
  122. Run the program.   Any surprizes?
  123.  
  124. ##### DO:
  125.  
  126. Change the Add to:
  127.  
  128. Add(2, 3, 4);
  129.  
  130. Run the program.   What happened?
  131.  
  132. The compiler refused to accept 4 as a variable identifier.  
  133.  
  134. The VAR preceding Sum in the procedure declaration puts a limit 
  135. on the corresponding parameter in the calling program: it has to 
  136. be a variable location to receive the value of Sum when the 
  137. procedure finishes its calculation.
  138.  
  139. The first two parameters in the procedure, No_1 and No_2, are 
  140. used only for input to the procedure - they do not provide any 
  141. output back to the corresponding parameters in the calling 
  142. program.
  143.  
  144. For this reason, the first two parameters in the calling program 
  145. are less restricted.  They can be constants, variables, or 
  146. expressions, as long as they are of the same type, integer, as 
  147. the corresponding parameters in the procedure.
  148. î
  149. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 68
  150.  
  151.  
  152. ##### DO:
  153.  
  154. Change the Add statement:
  155.  
  156. Add(4, 2 * Count + 5, Adjusted_Count);
  157.  
  158. Run the program.   Does the expression work o.k. as a parameter?
  159.  
  160. ##### DO:
  161.  
  162. Change the Add to:
  163.  
  164. Add(Count, Count, Count);
  165.  
  166. Also change the WriteLn to print the value of Count instead of 
  167. Adjusted_Count.
  168.  
  169. Run the program.   Any problems?
  170.  
  171.  
  172. 4.  A counter with error checking.
  173.  
  174. You could use the Procedure Add as a counter by using the 
  175. following call:
  176.  
  177. Add(Count, 1, Count);
  178.  
  179. The procedure would add 1 to Count and put the result in Count.
  180.  
  181. You could accomplish the same thing with the statement
  182.  
  183. Count := Count + 1;
  184.  
  185. So why bother to use the procedure?
  186.  
  187. What if Count reaches the upper limit of the integer range, 
  188. 32767?
  189.  
  190. In the main program you could expand the counting statement to:
  191.  
  192. IF Count < 32767
  193.    THEN
  194.      Count := Count + 1
  195.    ELSE
  196.      WriteLn('Counter reached upper limit');
  197. î
  198. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 69
  199.  
  200.  
  201. PROGRAMMING NOTE: HOW DO YOU DECIDE WHAT GOES INTO THE MAIN 
  202.                   PROGRAM AND WHAT TO PUT IN SUBPROGRAMS?  
  203.  
  204.                   FROM THE FOLLOWING DISCUSSION, TRY TO FORMULATE 
  205.                   AT LEAST ONE RULE FOR MAKING THIS DECISION.  
  206.  
  207. This looks a little messy, maybe you should use a procedure to 
  208. get this out of the main program.  
  209.  
  210. Not a bad reason, but there's a more important reason:
  211.  
  212. Ask yourself, "How much of the processing in the IF statement 
  213. above is of interest in the main program?"
  214.  
  215. Probably only the fact that a count is being incremented.  The 
  216. error checking and how it is done is probably of little interest 
  217. and just clutters up the main program.
  218.  
  219. ##### DO: 
  220.  
  221. Write your own procedure to increment the counter, Count.
  222.  
  223. Call the procedure, Increment.
  224.  
  225. Check for the upper limit of the integer range.  (The IF 
  226. statement above would be one way.)
  227.  
  228. Note that only one parameter is needed, preceded by VAR.
  229.  
  230. ##### DO:
  231.  
  232. In the main program, add the following to check out your 
  233. procedure:
  234.  
  235. FOR I := 1 to 10 DO
  236.   BEGIN
  237.     Increment(Count);
  238.     WriteLn('Count = ', Count);
  239.   END;
  240.  
  241. Run the program using the following values as input for count:
  242.  
  243. 0,  3,  -34, 32760
  244.  
  245. (PROG14A is provided in case you need help.)   
  246.  
  247. You could also write a procedure, Decrement, to decrease a 
  248. counter.  Note that the error checking would be checking the 
  249. lower limit, -32768, or perhaps 0 depending on how you intended 
  250. to use the counter.
  251. î
  252.  
  253.